from src.utils.method import Method
from src.exe_control import ExeControl
from src.bot.robot import Robot
from src.conf.conf_manager import ConfManager
from src.logger import Logger
from src.timer import Timer
from src.network.server import Server


class MyCli:
    def __init__(self):
        Method.ensure_utf8()
        self.print_motd()
        self.conf = ConfManager()
        self.logger = Logger(self.conf)
        self.timer = Timer(self.conf, self.logger)
        self.dls = ExeControl(self.conf, self.logger)
        self.bot = Robot(self.conf, self.logger, self.dls)
        self.server = Server(self.conf, self.logger, self.dls, self.bot)
        self.start()

    def do_event_loop(self) -> None:
        """命令行的支持"""
        while True:
            command = input()
            if command == "stop":
                self.stop()
                break
            elif command == "dlsload":
                self.reload()
            elif command == "start":
                self.dls.exe_start()
            elif command == "restart":
                self.dls.exe_restart()
            elif command == "startBak":
                self.dls.bak_world()
            else:
                self.dls.execute_cmd(command)

    def start(self) -> None:
        """启动所有模块"""
        self.logger.start()
        self.timer.start()
        self.dls.start_all()
        self.bot.start()
        self.server.start()

    def stop(self) -> None:
        """关闭所有模块"""
        self.dls.stop_all()
        self.bot.stop()

    def reload(self) -> None:
        """重载所有模块配置"""
        self.conf.reload()
        self.bot.reload()

    @staticmethod
    def print_motd() -> None:
        """打印欢迎信息"""
        print("[DLS] 当前程序版本『ws1.9.51』")
        print("[DLS] 使用说明参考https://gitee.com/dlcn/dlscq/wikis")
        print("[DLS] 如有问题与建议可加群531241108反馈")
        print("[DLS] 使用dlsload重新加载配置, start单独启动服务器")


if __name__ == '__main__':
    cli = MyCli()
    cli.do_event_loop()
